import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_mvvm_lite/core/state/page_state.dart';
import 'package:getx_mvvm_lite/features/todo/todo_view_model.dart';
import 'package:getx_mvvm_lite/shared/widgets/state_scaffold.dart';

class TodoPage extends StatelessWidget {
  const TodoPage({super.key});

  @override
  Widget build(BuildContext context) {
    final vm = Get.put(TodoViewModel());
    vm.loadInitial();

    return Obx(() => StateScaffold(
          state: vm.pageState.value,
          title: 'Todos',
          onRetry: vm.loadInitial,
          child: RefreshIndicator(
            onRefresh: vm.refresh,
            child: NotificationListener<ScrollNotification>(
              onNotification: (notification) {
                if (notification.metrics.pixels >=
                    notification.metrics.maxScrollExtent - 100) {
                  vm.loadMore();
                }
                return false;
              },
              child: ListView.separated(
                physics: const AlwaysScrollableScrollPhysics(),
                itemCount: vm.items.length,
                separatorBuilder: (_, __) => const Divider(height: 0),
                itemBuilder: (context, index) {
                  final todo = vm.items[index];
                  return ListTile(
                    title: Text(todo.title),
                    trailing: Icon(todo.completed ? Icons.check_circle : Icons.circle_outlined),
                  );
                },
              ),
            ),
          ),
        ));
  }
}